package com.zhan.ielts.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhan.frame.core.web.domain.CustomResult;
import com.zhan.ielts.domain.Users;
import com.zhan.ielts.domain.WordReview;
import com.zhan.ielts.domain.vo.WordReviewVo;
import com.zhan.ielts.service.UsersService;
import com.zhan.ielts.service.WordReviewService;

@Controller
@RequestMapping("/wordReview")
public class WordReviewController {

	private Logger log = LoggerFactory.getLogger(WordReviewController.class);

	@Autowired
	private WordReviewService wordReviewService;

	@Autowired
	private UsersService usersService;

	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public CustomResult update(String userId, String words, String device, String appVersion, String osVersion) {
		log.debug("更新单词复习开始");
		Map map = new HashMap();
		// 检查userId 是否为空
		if (userId == null || userId.trim().equals("")) {
			return new CustomResult(0, "userId为空！",map);
		}

		// 检查userId 是否有效
		Users query = new Users();
		query.setId(userId);
		Users users = usersService.queryOne(query);
		if (users == null) {
			return new CustomResult(0, "userId无效！",map);
		}

		try {
			JSONArray jan = JSONArray.fromObject(words);
			List<WordReviewVo> wordReviewList = JSONArray.toList(jan, WordReviewVo.class);
			// 更新答案
			wordReviewService.updateWordReview(wordReviewList, userId, device, appVersion, osVersion);
		} catch (Exception e) {
			return new CustomResult(0, "网络或系统错误！",map);
			
		}
		log.debug("更新单词复习结束");
//		return new Result(Status.OK, "");
		return new CustomResult(1, "",map);
	}

	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = "/remove", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public CustomResult remove(String userId) {
		log.debug("删除指定单词复习开始");
		Map map = new HashMap();
		// 检查userId 是否为空
		if (userId == null || userId.trim().equals("")) {
			return new CustomResult(0, "userId为空！",map);
		}

		// 检查userId 是否有效
		Users query = new Users();
		query.setId(userId);
		Users users = usersService.queryOne(query);
		if (users == null) {
			return new CustomResult(0, "userId无效！",map);
		}

		try {
			WordReview wordReview= new WordReview();
			wordReview.setUserId(userId);
			wordReviewService.removeByCustom(userId);
		} catch (Exception e) {
			return new CustomResult(0, "网络或系统错误！",map);
			
		}
		log.debug("删除指定单词复习结束");
		return new CustomResult(1, "",map);
	}

	@ResponseBody
	@RequestMapping(value = "/fetch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	// public Result fetch(String userId,Long updateTime,Integer count) {
	public CustomResult fetch(WordReviewVo data) {
		log.debug("获取答案开始");
		Map map = new HashMap();
		
		// 检查userId 是否为空
		if (data == null || data.getUserId() == null || data.getUserId().trim().equals("")) {
			return new CustomResult(0, "userId为空！",map);
		}

		// 检查userId 是否有效
		Users query = new Users();
		query.setId(data.getUserId());
		if (usersService.queryCount(query) < 1) {
			return new CustomResult(0, "userId无效！",map);
		}

		try {
			map = wordReviewService.fetchWordReviews(data);
			log.debug("获取答案结束");
		} catch (Exception e) {
			return new CustomResult(0, "网络或系统错误！",map);
		}

		return new CustomResult(1,"", map);
	}

}
